﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using RapChieuPhim.BUS;
using System.Windows.Forms;

namespace RapChieuPhim.DAO
{
    class VeDAO
    {
        public VeDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaVe", id);
            DataTable dt = cn.SqlExcuteQuery("LayVeTheoID", param);
            VeDTO Ve = new VeDTO();

            if (dt.Rows.Count != 0)
            {
                Ve.MaVe = int.Parse(dt.Rows[0]["MaVe"].ToString());
                Ve.ViTriGhe = dt.Rows[0]["ViTriGhe"].ToString();
                Ve.TrangThai = Boolean.Parse(dt.Rows[0]["TrangThai"].ToString());
                Ve.GiaTien = int.Parse(dt.Rows[0]["GiamGia"].ToString());
                Ve.Phim = PhimBUS.GetByID(int.Parse(dt.Rows[0]["MaPhim"].ToString()));
                Ve.SuatChieu = SuatChieuBUS.GetByID(int.Parse(dt.Rows[0]["MaSuatChieu"].ToString()));
                Ve.DoiTuong = DoiTuongBUS.GetByID(int.Parse(dt.Rows[0]["MaDoiTuong"].ToString()));
                Ve.Ngaychieu = DateTime.Parse(dt.Rows[0]["NgayChieu"].ToString());
                Ve.ThanhVien = TheThanhVienBUS.GetByID(dt.Rows[0]["MaThe"].ToString());
            }
            return Ve;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllVe");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    VeDTO Ve = new VeDTO();
                    Ve.MaVe = int.Parse(dr["MaVe"].ToString());
                    Ve.ViTriGhe = dr["ViTriGhe"].ToString();
                    Ve.TrangThai = Boolean.Parse(dr["TrangThai"].ToString());
                    Ve.GiaTien = int.Parse(dr["ViTriGhe"].ToString());
                    Ve.Phim = PhimBUS.GetByID(int.Parse(dr["MaPhim"].ToString()));
                    Ve.SuatChieu = SuatChieuBUS.GetByID(int.Parse(dr["MaSuatChieu"].ToString()));
                    Ve.DoiTuong = DoiTuongBUS.GetByID(int.Parse(dr["MaDoiTuong"].ToString()));
                    Ve.Ngaychieu = DateTime.Parse(dr["NgayChieu"].ToString());
                    Ve.ThanhVien = TheThanhVienBUS.GetByID(dr["MaThe"].ToString());
                    array.Add(Ve);
                }
            }
            return array;
        }

        public List<String> GetAllByGhe(int masuat, int maphim, DateTime ngay)
        {
            DataProvider cn = new DataProvider();
            List<String> array = new List<String>();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaSuat", masuat);
            param[1] = new SqlParameter("@MaPhim", maphim);
            param[2] = new SqlParameter("@NgayChieu", ngay.ToShortDateString());
            DataTable dt = cn.SqlExcuteQuery("LayAllVeTheoGhe", param);

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string ghe = dr["ViTriGhe"].ToString();
                    array.Add(ghe.Trim(' '));
                }
            }
            return array;
        }

        public void Insert(VeDTO Ve)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[8];
            param[0] = new SqlParameter("@ViTriGhe", Ve.ViTriGhe);
            param[1] = new SqlParameter("@TrangThai", Ve.TrangThai);
            param[2] = new SqlParameter("@GiaTien", Ve.GiaTien);
            param[3] = new SqlParameter("@MaPhim", Ve.Phim.MaPhim);
            param[4] = new SqlParameter("@MaSuatChieu", Ve.SuatChieu.MaSuatChieu);
            param[5] = new SqlParameter("@MaDoiTuong", Ve.DoiTuong.MaDoiTuong);
            param[6] = new SqlParameter("@NgayChieu", Ve.Ngaychieu);
            param[7] = new SqlParameter("@MaThe", Ve.ThanhVien == null ? DBNull.Value : (object)Ve.ThanhVien.MaThe);
            cn.SqlExcuteNonQuery("InsertVe", param);
        }

        public void Update(VeDTO Ve)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@MaVe", Ve.MaVe);
            param[1] = new SqlParameter("@ViTriGhe", Ve.ViTriGhe);
            param[2] = new SqlParameter("@TrangThai", Ve.TrangThai);
            param[3] = new SqlParameter("@GiaTien", Ve.GiaTien);
            param[4] = new SqlParameter("@MaPhim", Ve.Phim.MaPhim);
            param[5] = new SqlParameter("@MaSuatChieu", Ve.SuatChieu.MaSuatChieu);
            param[6] = new SqlParameter("@MaDoiTuong", Ve.DoiTuong.MaDoiTuong);
            param[7] = new SqlParameter("@NgayChieu", Ve.Ngaychieu);
            param[8] = new SqlParameter("@MaThe", Ve.ThanhVien == null ? DBNull.Value : (object)Ve.ThanhVien.MaThe);
            cn.SqlExcuteNonQuery("UpdateVe", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaVe", id);
            cn.SqlExcuteNonQuery("DeleteVe", param);
        }
    }
}
